﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace EEPClientDBSocket
{
    public partial class frmOption : Form
    {
        public frmOption()
        {
            InitializeComponent();
        }

        private void frmOption_Load(object sender, EventArgs e)
        {
            string s = Application.StartupPath + "\\EEPClientDBSocketOptions.xml";
            if (File.Exists(s))
            {
                XmlDocument DBXML = new XmlDocument();
                FileStream aFileStream = new FileStream(s, FileMode.Open, FileAccess.Read, FileShare.None);
                DBXML.Load(aFileStream);
                XmlNode aNode = DBXML.DocumentElement.FirstChild;
                try
                {
                    this.tbServerIP.Text = aNode.Attributes["ServerIP"].Value;
                    this.tbPort.Text = aNode.Attributes["Port"].Value;
                    this.tbUserID.Text = aNode.Attributes["UserID"].Value;
                    this.tbPassword.Text = aNode.Attributes["Password"].Value;
                    this.tbTimer.Text = aNode.Attributes["Timer"].Value;
                    this.tbSocketTime.Text = aNode.Attributes["SocketTime"].Value;
                    this.tbTimeOut.Text = aNode.Attributes["TimeOut"].Value;
                    if (aNode.Attributes["StartActive"].Value.ToLower() == "false")
                        this.cbStartActive.Checked = false;
                    else
                        this.cbStartActive.Checked = true;
                    if (aNode.Attributes["SynchroLocalTime"].Value.ToLower() == "false")
                        this.cbSynchroLocalTime.Checked = false;
                    else
                        this.cbSynchroLocalTime.Checked = true;
                }
                finally
                {
                    aFileStream.Close();
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            String s = Application.StartupPath + "\\EEPClientDBSocketOptions.xml";
            XmlDocument DBXML = new XmlDocument();
            FileStream aFileStream;
            String ServerIP = this.tbServerIP.Text;
            String Port = this.tbPort.Text;
            String UserID = this.tbUserID.Text;
            String Password = this.tbPassword.Text;
            String Timer = this.tbTimer.Text;
            String SocketTime = this.tbSocketTime.Text;
            String TimeOut = this.tbTimeOut.Text;
            String StartActive = this.cbStartActive.Checked.ToString();
            String SynchroLocalTime = this.cbSynchroLocalTime.Checked.ToString();

            if (!File.Exists(s))
            {
                try
                {
                    aFileStream = new FileStream(s, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    try
                    {
                        XmlTextWriter w = new XmlTextWriter(aFileStream, new System.Text.ASCIIEncoding());
                        w.Formatting = Formatting.Indented;
                        w.WriteStartElement("InfolightEEPClientDBSocketOptions");
                        w.WriteEndElement();
                        w.Close();
                    }
                    finally
                    {
                        aFileStream.Close();
                    }
                }
                catch (Exception er) { string str = er.Message; }
            }

            try
            {
                aFileStream = new FileStream(s, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                try
                {
                    DBXML.Load(aFileStream);
                    XmlNode aNode = null;

                    aNode = DBXML.DocumentElement.ChildNodes[0];

                    if (aNode == null)
                    {
                        XmlElement elem = DBXML.CreateElement("String");
                        XmlAttribute attr = DBXML.CreateAttribute("ServerIP");
                        attr.Value = ServerIP;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("Port");
                        attr.Value = Port;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("UserID");
                        attr.Value = UserID;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("Password");
                        attr.Value = Password;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("Timer");
                        attr.Value = Timer;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("SocketTime");
                        attr.Value = SocketTime;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("TimeOut");
                        attr.Value = TimeOut;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("StartActive");
                        attr.Value = StartActive;
                        elem.Attributes.Append(attr);

                        attr = DBXML.CreateAttribute("SynchroLocalTime");
                        attr.Value = SynchroLocalTime;
                        elem.Attributes.Append(attr);

                        DBXML.DocumentElement.AppendChild(elem);
                    }
                    else
                    {
                        aNode.Attributes["ServerIP"].InnerText = ServerIP;
                        aNode.Attributes["Port"].InnerText = Port;
                        aNode.Attributes["UserID"].InnerText = UserID;
                        aNode.Attributes["Password"].InnerText = Password;
                        aNode.Attributes["Timer"].InnerText = Timer;
                        aNode.Attributes["SocketTime"].InnerText = SocketTime;
                        aNode.Attributes["TimeOut"].InnerText = TimeOut;
                        aNode.Attributes["StartActive"].InnerText = StartActive;
                        aNode.Attributes["SynchroLocalTime"].InnerText = SynchroLocalTime;
                    }
                }
                finally
                {
                    aFileStream.Close();
                }
                DBXML.Save(s);
            }
            catch (Exception er) { string str = er.Message; }
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}